package study;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RandomDate {
    /**
     * 生成随机时间
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    public static Date randomDate(String beginDate, String endDate) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date start = format.parse(beginDate);
            Date end = format.parse(endDate);
            long date = random(start.getTime(), end.getTime());
            return new Date(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static long random(long begin, long end) {
        long rtn;
        if(begin==end){
            rtn = begin + (long) (Math.random() * 86399999);
        }else{
            rtn = begin + (long) (Math.random() * (end - begin));
        }
        return rtn;
    }

    public static void main(String[] args) {
        Date randomDate = randomDate("2010-09-20", "2010-09-20");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String result = format.format(randomDate);
        System.out.println(result);
        System.out.println(Long.parseLong("0.123450"));
    }
}
